// 
// $Rev:: 28            $:  Revision of last commit
// $Author:: rvhaverm@g#$:  Author of last commit
// $Date:: 2010-07-26 1#$:  Date of last commit
// 
package agents.agents;

import com.ibm.aglet.*;

import java.net.MalformedURLException;
import java.net.URL;

import agents.aglets.AgletBridge;
import agents.exceptions.ExceptionFactory;
import agents.exceptions.IllegalCodeBaseException;


/**
 * A Class that cannot be instantiated, providing the user with class-methods
 * that can create different types of agents.
 * 
 * @author Ruben Van Havermaet
 * @version 0.2
 * @see agents.aglets.AgletBridge
 */
public class AgentFactory
{

    /**
     * Constructor private: this class should not be instantiated.
     */
    private AgentFactory()
    {
    }

    /**
     * Create a new agent in a given context specified by its Classname and path
     * to the code-base.
     * 
     * @param cxt
     *            the AgletContext in which the agent should be created
     * @param init
     *            an Object initialization-object.
     * @param code
     *            String representing the path to the code
     * @param name
     *            String representing the agent's classname
     * @return an AgletProxy that serves as a representative for the newly
     *         created agent.
     * @throws IllegalCodeBaseException
     *             An exception describing that the URL to the codebase is
     *             illegal.
     */
    private static AgletProxy newAgent(AgletContext cxt, Object init,
	    String code, String name) throws IllegalCodeBaseException
    {

	URL code_base = null;
	try
	{
		if (code != null) code_base = new URL(code);
	}
	catch (MalformedURLException e)
	{
	    throw ExceptionFactory.newIllegalCodeBaseException("Cannot create "
		    + name + ". Illegal Path.");

	}
	return AgletBridge.createAgent(cxt, code_base, name, init);
    }

    // ***********************************************************************
    /**
     * Creates a new Receptionist and returns an AgletProxy that serves as a
     * representative of the Receptionist.
     * 
     * @param cxt
     *            the AgletContext in which to create the agent.
     * @param uiAgent
     *            an AgletProxy referring to the agent responsible for the
     *            UserInterface.
     * @return an AgletProxy that serves as a representative for the newly
     *         created Receptionist.
     * @throws IllegalCodeBaseException
     *             An exception describing that the URL to the codebase is
     *             illegal.
     * @see agents.agents.Receptionist
     */
    public static AgletProxy newReceptionist(AgletContext cxt,
	    AgletProxy uiAgent) throws IllegalCodeBaseException
    {

	// TODO: take location of agent class files from configuration
	// default to file://localhost
    	String code = "file://localhost/Receptionist.class";
    	//String code = null;
	try
	{
	    return newAgent(cxt, uiAgent, code, "agents.agents.Receptionist");
	}
	catch (IllegalCodeBaseException e)
	{
	    throw e;
	}
    }

    // ***********************************************************************

    public static AgletProxy newMobileAgent(AgletContext cxt)
	    throws IllegalCodeBaseException
    {

	// TODO: take location of agent class files from configuration
	// default to file://localhost
	String code = "file://localhost/MobileServiceAgent.class";
	try
	{
	    return newAgent(cxt, null, code, "agents.agents.MobileServiceAgent");
	}
	catch (IllegalCodeBaseException e)
	{
	    throw e;
	}
    }
    // ***********************************************************************
    // /** Creates a new GUIAgent and returns an AgletProxy object to this
    // * GUIAgent.
    // *
    // * @param cxt the AgletContext in which to create the GUIAgent.
    // * @param init the initializing Object, given to the agent's
    // * onCreation-method.
    // * @return an AgletProxy that serves as a representative for the newly
    // * created GUIAgent.
    // * @throws IllegalCodeBaseException An exception describing that the URL
    // * to the codebase is illegal.
    // * @see agents.agents.GUIAgent
    // */
    // public static AgletProxy newGUIAgent(AgletContext cxt, Object init)
    // throws IllegalCodeBaseException {
    // String code = "file://localhost/GUIAgent.java";
    // try {
    // return newAgent(cxt, init, code, "agents.agents.GUIAgent");
    // } catch (IllegalCodeBaseException e) {
    // throw e;
    // }
    // }
    //  
    //  
    // /** Creates a new TUIAgent and returns an AgletProxy object to this
    // * TUIAgent.
    // *
    // * @param cxt the AgletContext in which to create the TUIAgent.
    // * @param init the initializing Object, given to the agent's onCreation-
    // * method.
    // * @return an AgletProxy that serves as a representative for the newly
    // * created TUIAgent.
    // * @throws IllegalCodeBaseException An exception describing that the URL
    // * to the codebase is illegal.
    // * @see agents.agents.TUIAgent
    // */
    // public static AgletProxy newTUIAgent(AgletContext cxt, Object init)
    // throws IllegalCodeBaseException {
    // String code = "file://localhost/TUIAgent.java";
    // try {
    // return newAgent(cxt, init, code, "agents.agents.TUIAgent");
    // } catch (IllegalCodeBaseException e) {
    // throw e;
    // }
    // }
    // //
    // ***********************************************************************
    //  
    //  
    // //
    // ***********************************************************************
    // /** Creates a new SearchAgent and returns an AgletProxy object that
    // refers
    // * to this SearchAgent.
    // *
    // * @param cxt the AgletContext in which to create the agent.
    // * @param init an Object initialization-object
    // * @return an AgletProxy that serves as a representative for the newly
    // * created SearchAgent.
    // * @throws IllegalCodeBaseException An exception describing that the URL
    // * to the codebase is illegal.
    // * @see agents.agents.SearchAgent
    // */
    // public static AgletProxy newSearchAgent(AgletContext cxt, Object init)
    // throws IllegalCodeBaseException {
    // String code = "file://localhost/SearchAgent.java";
    // try {
    // return newAgent(cxt, init, code, "agents.agents.SearchAgent");
    // } catch (IllegalCodeBaseException e) {
    // throw e;
    // }
    // }
    //  
    //  
    // /** Creates a new ErrandsAgent and returns an AgletProxy object that
    // refers
    // * to this ErrandsAgent.
    // *
    // * @param cxt the AgletContext in which to create the agent.
    // * @param init an Object initialization-object
    // * @return an AgletProxy that serves as a representative for the newly
    // * created ErrandsAgent.
    // * @throws IllegalCodeBaseException An exception describing that the URL
    // to
    // * the codebase is illegal.
    // * @see agents.agents.ErrandsAgent
    // */
    // public static AgletProxy newErrandsAgent(AgletContext cxt, Object init)
    // throws IllegalCodeBaseException {
    // String code = "file://localhost/ErrandsAgent.java";
    // try {
    // return newAgent(cxt, init, code, "agents.agents.ErrandsAgent");
    // } catch (IllegalCodeBaseException e) {
    // throw e;
    // }
    // }
    // //
    // ***********************************************************************
    //  
    //  
    // //
    // ***********************************************************************
    // /** Creates a new TourismManager and returns an AgletContext referring to
    // * this TourismManager.
    // *
    // * @param cxt the AgletContext in which to create the TourismManager.
    // * @param init an Object initialization-object.
    // * @return an AgletProxy that serves as a representative for the newly
    // * created TourismManager.
    // * @throws IllegalCodeBaseException An exception describing that the URL
    // to
    // * the codebase is illegal.
    // * @see agents.agents.TourismManager
    // */
    // public static AgletProxy newTourismManager(AgletContext cxt, Object init)
    // throws IllegalCodeBaseException {
    // String code = "file://localhost/TourismManager.java";
    // try {
    // return newAgent(cxt, init, code, "agents.agents.TourismManager");
    // } catch (IllegalCodeBaseException e) {
    // throw e;
    // }
    // }
    //  
    //  
    // /** Creates a new HotelManager and returns an AgletContext referring to
    // * this HotelManager.
    // *
    // * @param cxt the AgletContext in which to create the HotelManager.
    // * @param init an Object initialization-object.
    // * @return an AgletProxy that serves as a representative for the newly
    // * created HotelManager.
    // * @throws IllegalCodeBaseException An exception describing that the URL
    // * to the codebase is illegal.
    // * @see agents.agents.HotelManager
    // */
    // public static AgletProxy newHotelManager(AgletContext cxt, Object init)
    // throws IllegalCodeBaseException {
    // String code = "file://localhost/HotelManager.java";
    // try {
    // return newAgent(cxt, init, code, "agents.agents.HotelManager");
    // } catch (IllegalCodeBaseException e) {
    // throw e;
    // }
    // }
    //  
    //  
    // /** Creates a new TransportManager and returns an AgletContext referring
    // to
    // * this TransportManager.
    // *
    // * @param cxt the AgletContext in which to create the TransportManager.
    // * @param init an Object initialization-object.
    // * @return an AgletProxy that serves as a representative for the newly
    // * created TransportManager.
    // * @throws IllegalCodeBaseException An exception describing that the URL
    // to
    // * the codebase is illegal.
    // * @see agents.agents.TransportManager
    // */
    // public static AgletProxy newTransportManager(AgletContext cxt, Object
    // init)
    // throws IllegalCodeBaseException {
    // String code = "file://localhost/TransportManager.java";
    // try {
    // return newAgent(cxt, init, code, "agents.agents.TransportManager");
    // } catch (IllegalCodeBaseException e) {
    // throw e;
    // }
    // }
    //  
    //  
    // /** Creates a new DBAgent and returns an AgletProxy representing this
    // * DBAgent.
    // *
    // * @param cxt the AgletContext in which to create the DBAgent.
    // * @param init an Object initialization-object.
    // * @return an AgletProxy that serves as a representative for the newly
    // * created DBAgent.
    // * @throws IllegalCodeBaseException An exception describing that the URL
    // to
    // * the codebase is illegal.
    // * @see agents.agents.DBAgent
    // */
    // public static AgletProxy newDatabaseAgent(AgletContext cxt, Object init)
    // throws IllegalCodeBaseException {
    // String code = "file://localhost/DBAgent.java";
    // try {
    // return newAgent(cxt, init, code, "agents.agents.DBAgent");
    // } catch (IllegalCodeBaseException e) {
    // throw e;
    // }
    // }
    // ***********************************************************************
}
